<?php

namespace App\Logics;


use App\Models\User;
use App\Models\UserAuth;
use App\Models\UserFan;
use App\Service\Base\Logic;

class FanLogic extends Logic
{

    // 粉丝：关注学校机构（认证的用户）

    // 粉丝列表：某个机构的粉丝信息
    public function getFans($rows = 0)
    {
        $option  = $this->option();
        $page    = $option->getData('page', 1, 'intval');
        $rows    = $rows ?: $option->getData('rows', 15, 'intval');
        $auth_id = $option->getData('auth_id', 0, 'intval');
        $fanDb   = new UserFan();
        $result  = $fanDb->bangField(['auth_id'])->listPage(['auth_id' => $auth_id], $page, $rows);

        // 整理用户信息
        $result['list'] = $this->listsAddUser($result['list']);

        return $result;
    }


    // 关注列表：某个人的
    public function onLists($myUid)
    {
        $option = $this->option();
        $page   = $option->getData('page', 1, 'intval');
        $rows   = $option->getData('rows', 15, 'intval');
        $fanDb  = new UserFan();
        $result = $fanDb->bangField(['uid'])->listPage(['uid' => $myUid], $page, $rows);

        // 整理收藏的活动
        //   以后可能会收藏文章等
        if(!empty($result['list'])){
            $aids  = array_column($result['list'], 'auth_id');
            $auths = (new UserAuth())->getListInIds($aids);
            foreach($result['list'] as $key => $item){
                $result['list'][$key]['auth'] = $auths[$item['auth_id']] ?? [];
            }
        }

        return $result;
    }

    // 关注操作（未使用）
    public function _onFollow($myUid)
    {
        $option  = $this->option();
        $auth_id = $option->getData('auth_id', 0, 'intval');
        if(empty($auth_id)){
            return $this->_returnFail('请指定要关注的认证编号！');
        }

        $fanDb = new UserFan();
        $res   = $fanDb->onFollow($myUid, $auth_id);
        if($res === false){
            return $this->_returnFail($fanDb->getError());
        }

        return $res;
    }

}


